#include <stdio.h>
#include <stdlib.h>

int main(void) {
    double num1, num2, result;
    char op, choice;

    printf("=== 简单计算器 ===\n");
    printf("支持 + - * / 操作\n");
    printf("输入 'q' 退出\n\n");

    while (1) {
        printf("请输入第一个数: ");
        if (scanf("%lf", &num1) != 1) {
            // 处理非数字输入
            while (getchar() != '\n');  // 清空缓冲区
            printf("无效输入，请重试。\n\n");
            continue;
        }

        printf("请输入运算符 (+ - * /): ");
        scanf(" %c", &op);  // 空格忽略前导空白
        if (op == 'q' || op == 'Q') break;

        printf("请输入第二个数: ");
        if (scanf("%lf", &num2) != 1) {
            while (getchar() != '\n');
            printf("无效输入，请重试。\n\n");
            continue;
        }

        // 执行计算
        switch (op) {
            case '+': result = num1 + num2; break;
            case '-': result = num1 - num2; break;
            case '*': result = num1 * num2; break;
            case '/':
                if (num2 == 0) {
                    printf("错误: 除数不能为0！\n\n");
                    continue;
                }
                result = num1 / num2; break;
            default:
                printf("无效运算符，请重试。\n\n");
                continue;
        }

        printf("结果: %.2f\n\n", result);

        printf("继续计算？(y/n): ");
        scanf(" %c", &choice);
        if (choice != 'y' && choice != 'Y') break;
    }

    printf("计算结束，再见！\n");
    return 0;
}